//
//  ZLookFriendViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/17.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZLookFriendViewController.h"
#import "ZLookFriendTableViewCell.h"
#import "ZLookSearchView.h"


#import "ZLookFriendDeatilViewController.h" // 查看详情界面
#import "SearchViewController.h"

#import "ZCVModel.h"
#import "ZillFModel.h"
#import "SPPlaceholderCell.h"
static NSString *const placeCellID = @"placeholders";

static int  lookp = 1; // 看看病友分页
static NSString * const kZLookFriendIdentifier = @"kZLookFriendIdentifier" ;
@interface ZLookFriendViewController ()<UITableViewDelegate,UITableViewDataSource>{
    ZLookSearchView * _headView;
    
}
@property (nonatomic,strong) UITableView * mainTableView;
@property (nonatomic,strong) NSMutableArray <ZCVModel *> * cvModelArray;
@property (nonatomic,strong) NSMutableArray <ZillFModel *> *
zIllModelArray;
@property (nonatomic,strong) LZUserInfo * userInfo;
@property (nonatomic,assign) BOOL isHaveData;
@end

@implementation ZLookFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"看看病友";
    [self initialzeDatas];
    [self initlizeInterface];
}
- (void)initialzeDatas {
    self.cvModelArray = [NSMutableArray array];
    self.zIllModelArray = [NSMutableArray array];
    _userInfo = [LZUserInfo getUserInfo];
    self.isHaveData = YES;
}
- (void)initlizeInterface {
    ESWeak(self, iWeakSelf);
    self.view.backgroundColor = [UIColor whiteColor];
    _headView = [[ZLookSearchView alloc] init];
    _headView.backgroundView.backgroundColor = MYGRAYCOLOR;
    [self.view addSubview:_headView];
    [self.view addSubview:self.mainTableView];
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.width.equalTo(self.view);
        make.height.offset(50*ysIZE);
    }];
    [_mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(self.view);
        make.top.equalTo(_headView.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
    [_headView.searchBtn addTarget:self action:@selector(jumpSearchAction:) forControlEvents:UIControlEventTouchUpInside];
    _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [iWeakSelf lookNetWorking];
        [_mainTableView.mj_header endRefreshing];
    }];
    _mainTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [iWeakSelf moreLookNetWorking];
        [_mainTableView.mj_footer endRefreshing];
    }];
    [_mainTableView.mj_header beginRefreshing];
    
}
// 跳转到搜索界面
- (void)jumpSearchAction:(UIButton *)sender {
    SearchViewController * searchVC = [[SearchViewController alloc] init];
    [self .navigationController pushViewController:searchVC animated:YES];
}
// 表格视图协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.isHaveData ? self.cvModelArray.count : 1 ;
//    return (self.cvModelArray&& self.cvModelArray.count != 0) ? self.cvModelArray.count : 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.isHaveData ? 270 *ysIZE : SCREEN_Height - 104;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHaveData == YES) {
        ZLookFriendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kZLookFriendIdentifier forIndexPath:indexPath];
        cell.cvModel = self.cvModelArray[indexPath.row];
        cell.illModel = self.zIllModelArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

    }else {
        SPPlaceholderCell *cell = [tableView dequeueReusableCellWithIdentifier:placeCellID];
        cell.selectionStyle = 0;
        return cell;
    }
   }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHaveData == YES) {
        ZLookFriendDeatilViewController * ZLFDVC = [[ZLookFriendDeatilViewController alloc] init];
        ZLFDVC.qId = self.zIllModelArray[indexPath.row].qid;
        ZLFDVC.zLookCV = self.cvModelArray[indexPath.row];
        ZLFDVC.ishiden = NO;
        [self.navigationController pushViewController:ZLFDVC animated:YES];
        return;
    }else {
        return;
    }
    
}

- (void)lookNetWorking {
    ESWeak(self, iWeakself);
    lookp = 1;
    self.isHaveData = YES;
    [YJProgressHUD showProgress:@"正在刷新" inView:self.view];
    NSString * p = [NSString stringWithFormat:@"%d",lookp];
    NSMutableDictionary * dict = [@{@"p":p} mutableCopy];
    if (_userInfo.uid && ![_userInfo.uid isEqualToString:@""]) {
        [dict setObject:_userInfo.uid forKey:@"uid"];
    }
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Index/illFriend" sucessBlock:^(id content) {
        NSDictionary * iDict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"%@",iDict);
        if ([iDict[@"code"] intValue] == 1 ) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
             [iWeakself disposeDataWithArray:iDict[@"list"]];
        }else {
            self.isHaveData = NO;
            [self.mainTableView reloadData];
            [YJProgressHUD showMessage:@"刷新失败" inView:self.view];
        }
       
    } failure:^(NSError *error) {
        self.isHaveData = NO;
        [self.mainTableView reloadData];
        [YJProgressHUD showMessage:@"网络请求错误" inView:self.view];
    }];
}

- (void)disposeDataWithArray:(NSArray * )array {
    [self.mainTableView.mj_footer resetNoMoreData];
    [self.cvModelArray removeAllObjects];
    [self.zIllModelArray removeAllObjects];
    if (array.count == 0) {
        self.isHaveData = NO;
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [_mainTableView.mj_footer endRefreshingWithNoMoreData];
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [_mainTableView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSDictionary * dict in array) {
        ZCVModel * vcModel = [[ZCVModel alloc] initWithDict:[dict mutableCopy]];
        ZillFModel * illModel = [[ZillFModel alloc] initWithDict:[dict mutableCopy]];
        [self.cvModelArray addObject:vcModel];
        [self.zIllModelArray addObject:illModel];
    }
        [self.mainTableView reloadData];
    
}

- (void)moreLookNetWorking {
    lookp ++;
    ESWeak(self, iWeakSelf);
    NSString * p = [NSString stringWithFormat:@"%d",lookp];
    NSMutableDictionary * dict = [@{@"p":p} mutableCopy];
    if (_userInfo.uid && ![_userInfo.uid isEqualToString:@""]) {
        [dict setObject:_userInfo.uid forKey: @"uid"];
    }
    
    [YJProgressHUD showProgress:@"正在加载" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Index/illFriend" sucessBlock:^(id content) {
        NSDictionary * idict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([idict[@"code"]  intValue] == 1) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
            [iWeakSelf moreDisposeDataWithArray:idict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"刷新失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求错误" inView:self.view];
    }];
}
- (void)moreDisposeDataWithArray:(NSArray * )array {
    [self.mainTableView.mj_footer resetNoMoreData];
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [_mainTableView.mj_footer endRefreshingWithNoMoreData];
        
    }else if (array.count < 10) {
        [_mainTableView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSMutableDictionary * dict in array) {
        ZCVModel * vcModel = [[ZCVModel alloc] initWithDict:dict];
        ZillFModel * illModel = [[ZillFModel alloc] initWithDict:dict];
        [self.cvModelArray addObject:vcModel];
        [self.zIllModelArray addObject:illModel];
    }
    [self.mainTableView reloadData];
   
}
// lazy
-(UITableView *)mainTableView {
    if (_mainTableView) {
        return _mainTableView;
    }
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
//    _mainTableView.rowHeight = 270 *ysIZE;
    _mainTableView.separatorStyle = NO;
    [_mainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPPlaceholderCell class]) bundle:nil] forCellReuseIdentifier:placeCellID];
    [_mainTableView registerClass:[ZLookFriendTableViewCell class] forCellReuseIdentifier:kZLookFriendIdentifier];
    
    return _mainTableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
